package com.example.xue.json;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         Weatherinfo weatherinfo = new Weatherinfo();
//        List<Weatherinfo> listWeatherinfo = new ArrayList<Weatherinfo>();
//        for(int i=0;i<5;i++) {
//            Weatherinfo stu = new Weatherinfo();
//            listWeatherinfo.add(stu);
//        }
        EditText mCity ;
        mCity = (EditText) findViewById(R.id.editText);
        RequestQueue mQueue;

        mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.weather.com.cn/adat/cityinfo/101010100.html", null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());

//                        List<Weatherinfo> list = new ArrayList<Weatherinfo>();
                        Log.d("TAG", response.toString());
                        String mJSON = response.toString();
                        Weatherinfo weatherinfo = new Weatherinfo();
//                        List<Weatherinfo> stu =JSON.parseObject(mJSON, new TypeReference<List<Weatherinfo>>(){});
                        weatherinfo = JSON.parseObject(mJSON, Weatherinfo.class);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("TAG", error.getMessage(), error);
                    }
                });

        mQueue.add(jsonObjectRequest);

//        mCity.setText(weatherinfo[0].getCity());


       json2BeanM2();




    }

    private static void json2BeanM2() {
        String s = "{\"id\":\"0375\",\"city\":\"平顶山\"}";
        //一个简单方便 的方法将Json文本信息转换为JsonObject对象的同时转换为JavaBean对象！
        Weibo weibo = JSON.parseObject(s, Weibo.class);//Weibo类在下边定义
        System.out.println(weibo.getId());
        System.out.println(weibo.getCity());
        //打印的结果     0375
        //              平顶山
    }

    public static <T> List<T> getBeans(String jsonString, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            list = JSON.parseArray(jsonString, cls);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
